import { setToken } from '@/utils/auth'
import request from '@/utils/request'

export function urlSetToken (emit){
  const URL = location.href
  let url = URL.split("?")[1];
  const urlSearchParams = new URLSearchParams(url);
  const params = Object.fromEntries(urlSearchParams.entries());
  return new Promise((resolve, reject) => {
    if(!params[emit]) return resolve(false)
    request({
      url: '/authLogin',
      method: 'get',
      headers: {
        isToken: false
      },
      params: {
        token: params[emit]
      }
    }).then(rs => {
      setToken(rs['token'])
      let newurl = `${location.origin}${location.pathname}`
      window.history.replaceState({
        path: newurl
      }, '', newurl);
      resolve(true)
    })
  })
}